from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
from database import Base

class TaskDrone(Base):
    """任务-飞机关联表"""
    __tablename__ = "task_drones"
    
    id = Column(Integer, primary_key=True, index=True)
    task_id = Column(Integer, ForeignKey('tasks.id'), nullable=False)
    drone_id = Column(Integer, ForeignKey('drones.id'), nullable=False)
    is_primary = Column(Boolean, default=False, comment="是否为主用飞机")
    status = Column(String(20), default="assigned", comment="状态：assigned, active, completed, failed")
    assigned_at = Column(DateTime, default=datetime.now, comment="分配时间")
    start_time = Column(DateTime, nullable=True, comment="开始执行时间")
    end_time = Column(DateTime, nullable=True, comment="结束时间")
    notes = Column(String(255), nullable=True, comment="备注")
    
    # 关系
    task = relationship("Task", backref="task_drones")
    drone = relationship("Drone")